﻿using System;
using System.Web;
using System.Web.Services;

namespace Hanhud.Web.FlashUpload.Handler
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class PreviewHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            string absoluteRootPath = context.Server.MapPath(string.IsNullOrEmpty(context.Request.QueryString["path"]) ? "~/FlashUpload/Temp/" : context.Request.QueryString["path"]);
            bool stored = false;
            if (!string.IsNullOrEmpty(context.Request.QueryString["fid"]))
            {
                if (!string.IsNullOrEmpty(context.Request.QueryString["stored"]) && bool.TryParse(context.Request.QueryString["stored"], out stored))
                {
                    string photoID = context.Request.QueryString["fid"];

                    byte[] photoContent = CPhoto.GetPhotoContent(photoID, false, context.Server.MapPath(Config.CSiteConfig.Current.ImageSection.StorePath));
                    context.Response.OutputStream.Write(photoContent, 0, photoContent.Length);
                    context.Response.End();
                }
                else
                {
                    string sid = string.IsNullOrEmpty(context.Request.QueryString["sid"]) ? string.Empty : context.Request.QueryString["sid"];
                    string fileName = sid + context.Request.QueryString["fid"].ToLower().Replace(' ', '_');
                    string filePath = absoluteRootPath + fileName;

                    if (System.IO.File.Exists(filePath))
                    {
                        try
                        {
                            string[] files = System.IO.Directory.GetFiles(absoluteRootPath);
                            for (int i = 0; i < files.Length; i++)
                                if (files[i].IndexOf(fileName) > -1)
                                {
                                    context.Response.Clear();

                                    string fileExt = System.IO.Path.GetExtension(files[i]);

                                    if (fileExt == ".jpg") context.Response.ContentType = "image/jpeg";
                                    else if (fileExt == ".gif") context.Response.ContentType = "image/gif";
                                    else if (fileExt == ".png") context.Response.ContentType = "image/png";
                                    else context.Response.ContentType = "image/bmp";

                                    System.IO.FileStream stream = System.IO.File.Open(files[i], System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
                                    byte[] contents = new byte[stream.Length];

                                    stream.Read(contents, 0, contents.Length);

                                    context.Response.OutputStream.Write(Hanhud.Utils.CHelper.ResizeImageFile(contents, 60), 0, contents.Length);
                                    context.Response.End();

                                    stream.Close();
                                    stream.Dispose();
                                    break;
                                }
                        }
                        catch (Exception ex) { string msg = ex.Message; }
                    }
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
